package models

import (
	"time"

	"gorm.io/gorm"
)

type Post struct {
	gorm.Model
	Title        string     //标题
	Content      string     //内容
	PubTime      *time.Time //发布时间
	CommentCount uint32     //默认是0
	CommentState string

	Comments []*Comment
	UserID   *uint
	User     *User
}

func (p *Post) AfterCreate(tx *gorm.DB) (err error) {
	var count int
	//查询最新数量
	tx.Raw("select count(*) from posts where user_id=? and deleted_at is null", p.UserID).Scan(&count)

	var user User
	tx.Model(&user).Where("id=?", p.UserID).Update("post_count", count)
	// +1
	return nil
}
